const menuModel = require("../model/menuModel.js")
class Ad {
	// 获取广告列表
	static async getMenu(ctx) {
		let {
			nowPage,
			pageSize
		} = ctx.request.query
		if (!nowPage) {
			nowPage = 1
		}
		if (!pageSize) {
			pageSize = 10
		}
		let res = await menuModel.getMenuList(nowPage, pageSize)
		ctx.body = {
			code: 0,
			codeInfo: "成功",
			data: res
		}
	}


	// 添加广告
	static async addMenu(ctx) {
		let {
			name,
			path,
			openurl,
			status
		} = ctx.request.body
		if (!name && !path && !openurl) {
			ctx.body = {
				code: 1,
				codeInfo: "参数错误"
			}
		} else {
			await menuModel.addMenu(name, path, openurl, status)
			ctx.body = {
				code: 0,
				codeInfo: "成功"
			}
		}

	}



	// 修改广告

	static async editMenu(ctx) {
		let {
			id,
			name,
			path,
			openurl,
			status
		} = ctx.request.body
		if (!name && !path && !openurl && !id) {
			ctx.body = {
				code: 1,
				codeInfo: "参数错误"
			}
		} else {
			await menuModel.editMenu(id, name, path, openurl, status)
			ctx.body = {
				code: 0,
				codeInfo: "成功"
			}
		}
	}



	// 删除 广告
	static async delMenu(ctx) {
		let {
			id
		} = ctx.request.query
		if (!id) {
			ctx.body = {
				code: 1,
				codeInfo: "参数错误"
			}
		} else {
			await menuModel.delMenu(id)
			ctx.body = {
				code: 0,
				codeInfo: "成功"
			}
		}
	}

}

module.exports = Ad